﻿using System.Web;

namespace Common
{
    public static class UrlHelper
    {
        /// <summary>
        /// 获取请求中的URL全路径+请求参数
        /// 如：http://www.MarketsWeb.com/Admin/Default.aspx?pid=1
        /// </summary>
        /// <param name="request">当前请求</param>
        /// <returns></returns>
        public static string UrlAndQuery(HttpRequest request)
        {
            return request.Url.AbsoluteUri;
        }
        /// <summary>
        ///  获取请求中的URL全路径，不带参数
        /// </summary>
        /// <param name="request">当前请求</param>
        /// <returns></returns>
        public static string Url(HttpRequest request)
        {
            if (request.Url.AbsoluteUri.IndexOf("?") > -1)
                return request.Url.AbsoluteUri.Substring(0, request.Url.AbsoluteUri.IndexOf("?") - 1);
            else
                return request.Url.AbsoluteUri;
        }
        /// <summary>
        /// 网站的路径，包含虚拟目录路径
        /// 格式: 协议 + "://" + 域名 (+ ":" + 端口) (+ 虚拟目录路径)
        /// 不以"/"结束
        /// </summary>
        public static string SiteUrl(HttpRequest request)
        {
            if (request.ApplicationPath == "/")
                return request.Url.Scheme + "://" + request.Url.Authority;
            else
                return request.Url.Scheme + "://" + request.Url.Authority + request.ApplicationPath;

        }
        /// <summary>
        /// 网站的根，不包含虚拟目录路径
        /// 格式: 协议 + "://" + 域名 (+ ":" + 端口)
        /// 不以"/"结束
        /// </summary>
        public static string SiteRoot(HttpRequest request)
        {
            return request.Url.Scheme + "://" + request.Url.Authority;
        }
        /// <summary>
        /// 获取请求中的当前页面
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string CurrentPage(HttpRequest request)
        {
            return request.ServerVariables["PATH_INFO"];
        }
        /// <summary>
        /// 获取请求中的当前页面(带参数)
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static string CurrentPageAndQuery(HttpRequest request)
        {
            return request.ServerVariables["PATH_INFO"] + request.Url.Query;
        }
        /// <summary>
        /// 获取请求中的URL全路径，不带ticket参数
        /// </summary>
        /// <param name="request">当前请求</param>
        /// <returns></returns>
        public static string TicketOutUrl(HttpRequest request)
        {
            string[] _url = request.Url.AbsoluteUri.Split('?', '&');
            string _ticket = string.Empty;
            foreach (string str in _url)
            {
                if (str.StartsWith("ticket"))
                    _ticket = str;
            }
            string url = request.Url.AbsoluteUri;
            if (!string.IsNullOrEmpty(_ticket))
            {
                if (url.Contains("?" + _ticket + "&"))
                {
                    url = url.Replace("?" + _ticket + "&", "?");
                }
                else if (url.Contains("&" + _ticket + "&"))
                {
                    url = url.Replace("&" + _ticket + "&", "&");
                }
                else if (url.Contains("?" + _ticket))
                {
                    url = url.Remove(url.IndexOf(_ticket) - 1);
                }
                else if (url.Contains("&" + _ticket))
                {
                    url = url.Remove(url.IndexOf(_ticket) - 1);
                }
            }
            return url;
        }
    }
}
